package com.moss.cloud.base.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.base.api.model.from.BaseFrom;
import com.moss.cloud.base.api.model.from.UserPositionFrom;
import com.moss.cloud.base.api.model.param.BasePageParam;
import com.moss.cloud.base.api.model.vo.UserPositionVO;
import com.moss.cloud.base.biz.mapper.SysUserPositionMapper;
import com.moss.cloud.base.biz.model.SysUserPosition;
import com.moss.cloud.base.biz.service.ISysUserPositionService;
import com.moss.cloud.base.biz.service.ISysUserService;
import com.moss.cloud.common.base.utils.PageUtil;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.security.annotation.RequiresTenant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户岗位业务实现
 *
 * @author 瑾年
 * @date 2023年12月24日
 */
@Slf4j
@Service
public class SysUserPositionServiceImpl extends ServiceImpl<SysUserPositionMapper, SysUserPosition> implements ISysUserPositionService {

    private final ISysUserService sysUserService;

    public SysUserPositionServiceImpl(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 指定用户职位查询
     *
     * @param basePageParam
     * @return
     */
    @Override
    @RequiresTenant
    public IPage<UserPositionVO> userPositionPage(BasePageParam basePageParam) {
        Page<UserPositionVO> page = PageUtil.createPage(basePageParam.getCurrent(), basePageParam.getSize());
        return this.baseMapper.userPositionPage(page, basePageParam.getId());
    }

    /**
     * 用户转岗
     *
     * @param userPositionFrom
     * @return
     */
    @Override
    @RequiresTenant
    @Transactional(rollbackFor = Exception.class)
    public boolean transfer(UserPositionFrom userPositionFrom) {
        this.sysUserService.userDataPermissions(userPositionFrom.getUserId());
        List<SysUserPosition> positions = this.list(new QueryWrapper<SysUserPosition>().lambda()
                .eq(SysUserPosition::getUserId, userPositionFrom.getUserId()));
        BooleanHandel.isTrue(CollectionUtils.isNotEmpty(positions)).trueHandle(() -> this.clearPosition(userPositionFrom.getUserId()));
        List<SysUserPosition> userPositions = userPositionFrom.getPositionIds().stream().map(item -> {
            SysUserPosition sysUserPosition = new SysUserPosition();
            sysUserPosition.setUserId(userPositionFrom.getUserId());
            sysUserPosition.setPositionId(item);
            return sysUserPosition;
        }).collect(Collectors.toList());
        return ((SysUserPositionServiceImpl) AopContext.currentProxy()).saveBatch(userPositions);
    }

    /**
     * 清除用户岗位
     *
     * @param userId
     * @return
     */
    @Override
    @RequiresTenant
    @Transactional(rollbackFor = Exception.class)
    public boolean clearPosition(Long userId) {
        return this.remove(new QueryWrapper<SysUserPosition>().lambda()
                .eq(SysUserPosition::getUserId, userId));
    }

    /**
     * 用户岗位
     *
     * @param baseFrom
     * @return
     */
    @Override
    @RequiresTenant
    public List<Long> userPositions(BaseFrom baseFrom) {
        List<SysUserPosition> sysUserPositions = this.list(new QueryWrapper<SysUserPosition>().lambda().eq(SysUserPosition::getUserId, baseFrom.getId()));
        return CollectionUtils.isNotEmpty(sysUserPositions) ? sysUserPositions.stream().map(SysUserPosition::getPositionId).collect(Collectors.toList()) :
                Collections.emptyList();
    }
}
